<?php

namespace app\api\controller\common;

use app\common\controller\Api;
use think\facade\Db;
use app\office\Import;
use app\service\Http;

/**
 * 跑数据使用的接口
 */
class Script extends Api
{
	protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    // 季华实验室 科研成果的导入接口
    public function patent()
    {
    	// 先上传
        $file = $this->request->file("file");

        if (empty($file)) {
            return ajaxReturn("请上传文件", 0);
        }
        
        // 上传到本地服务器
        $savename = \think\facade\Filesystem::putFile('uploads', $file);
        $root = $_SERVER['DOCUMENT_ROOT'];
        $savename = str_replace('\\', "/", $savename);
        $savename = $root.'/'.$savename;
        // 解析文件
        $data = Import::agentExamples($savename);

        if (empty($data)) {
        	return ajaxReturn("数据为空", 0);
        }
        foreach ($data as $k => $v) {
        	//是否与佛山产业匹配:0=无,1=有
        	if ($v['status']=='有') {
        		$data[$k]['status'] = 1;
        	}else{
        		$data[$k]['status'] = 0;
        	}
        	// 加上创建时间
        	$data[$k]['createtime'] = time();
        	// 去除空值
        	if (empty($v['title']) && empty($v['team_name']) && empty($v['people'])) {
        		unset($data[$k]);
        	}
        }
        Db::name('web_scientific')->insertAll($data);
        return ajaxReturn('导入成功',1);
    }

    //  用于跑示范企业的接口
    public function change()
    {
        // $list = Db::name('web_honor_examples')->where('type_id',10)->select()->toArray();
        // foreach ($list as $k => $v) {
        //     $temp = explode('（',$v['name']);
        //     if (isset($temp[0])) {
        //         $name = $temp[0];
        //     }else{
        //         $name = $v['name'];
        //     }
        //     if (isset($temp[1])) {
        //         $year = explode('年',$temp[1]);
        //         $years = $year[0];
        //     }else{
        //         $years = $v['years'];
        //     }
        //     $arr = [
        //         'name'=>$name,
        //         'years'=>$years,
        //     ];
        //     Db::name('web_honor_examples')->where('id',$v['id'])->update($arr);
            
        // }
        return ajaxReturn('执行成功',1);
    }

    // 导入公司接口
    public function company()
    {
        // 先上传
        $file = $this->request->file("file");

        if (empty($file)) {
            return ajaxReturn("请上传文件", 0);
        }
        
        // 上传到本地服务器
        $savename = \think\facade\Filesystem::putFile('uploads', $file);
        $root = $_SERVER['DOCUMENT_ROOT'];
        $savename = str_replace('\\', "/", $savename);
        $savename = $root.'/'.$savename;
        // 解析文件
        $data = Import::company($savename);

        if (empty($data)) {
            return ajaxReturn("数据为空", 0);
        }
        foreach ($data as $k => $v) {
            
            // 加上创建时间
            $data[$k]['createtime'] = date('Y-m-d',time());
            // 去除空值
            if (empty($v['company'])) {
                unset($data[$k]);
            }
        }
        Db::name('company')->insertAll($data);
        return ajaxReturn('导入成功',1);
    }

    // ====================================================
    // 和patent_lsit组合使用
    // 跑五大区专利总数和三种类型总数的
    public static function params($address,$pdt)
    {
        $url = 'https://patent.shundeip.com/v1/search/expression';
        $data = [
            'exp'=>'专利权人地址=佛山市'.$address,
            'pdt'=>$pdt,
        ];
        $result = Http::post($url,$data);
        $result = json_decode($result,true);
        if (empty($result['data']) || empty($result['data']['page'])) {
            $total = 0;
        }
        $total = isset($result['data']['page']['total'])?$result['data']['page']['total']:0;
        return $total;
    }

    // 跑各大区专利总数
    public function patent_list()
    {
        // 本月
        $month = date('Y-m',time());
        // $month = input('month') ?? '';

        // 五个区
        $address = Db::name('address')->where('id','>',0)->order('weigh desc')->select()->toArray();

        foreach ($address as $k => $v) {
            $count = $this->params($v['name'],'');
            $auth = $this->params($v['name'],'发明');
            $news = $this->params($v['name'],'实用新型');
            $apper = $this->params($v['name'],'外观设计');
            $arr = [
                'address_id'=>$v['id'],
                'date'=>$month,
                'auth'=>$auth,
                'news'=>$news,
                'apper'=>$apper,
                'count'=>$count,
                'createtime'=>date('Y-m-d H:i:s',time()),
            ];
            Db::name('data_patent')->insert($arr);
        }

        return ajaxReturn('执行成功',1);
    }

    //=================================================
    //3+3+x专利情况数据
    public static function params_industry($address_id,$industry_id,$pdt,$lssc)
    {
        $address = Db::name('address')->where('id',$address_id)->value('name');
        $url = 'https://patent.shundeip.com/v1/search/expression';
        // 拼接ipc
        $ipc = '';
        $no_ipc = '';
        $industry = Db::name('industry_ipc')
                    ->where('industry_id',$industry_id)
                    ->select()->toArray();
        foreach ($industry as $k => $v) {
            if ($v['is_remove']==0) {  //表示需要拼接的
                $temp = explode('、',$v['ipc']);
                foreach ($temp as $key => $value) {
                    $ipc .= 'IPC=\''.$value.'\' OR ';
                }
            }
            if ($v['is_remove']==1) {  //表示需要去除的
                $temp = explode('、',$v['ipc']);
                foreach ($temp as $key => $value) {
                    $no_ipc .= 'IPC=\''.$value.'\' OR ';
                }
            }
        }
        $ipc = substr_replace($ipc,'',-3);//去除最后的三个字符
        $no_ipc = substr_replace($no_ipc,'',-3);
        // var_dump($ipc);
        // var_dump($no_ipc);
        if (empty($no_ipc)) {
            $exp = '专利权人地址=佛山市'.$address.' AND ('.$ipc.')';
        }else{
            $exp = '专利权人地址=佛山市'.$address.' AND ('.$ipc.') NOT ('.$no_ipc.')';
        }
        // 判断是否有pdt和lssc
        if (empty($pdt) && empty($lssc)) {
            $data = [
                'exp'=>$exp,
            ];
        }else if(empty($pdt)){
            $data = [
                'exp'=>$exp,
                'lssc'=>$lssc,
            ];
        }else if(empty($lssc)){
            $data = [
                'exp'=>$exp,
                'pdt'=>$pdt,
            ];
        }else{
            $data = [
                'exp'=>$exp,
                'pdt'=>$pdt,
                'lssc'=>$lssc,
            ];
        }
        // var_dump($data);exit;
        $result = Http::post($url,$data);
        $result = json_decode($result,true);
        if (empty($result['data']) || empty($result['data']['page'])) {
            $total = 0;
        }

        $total = isset($result['data']['page']['total'])?$result['data']['page']['total']:0;
        // var_dump($total);exit;
        return $total;
    }

    public function industry_list()
    {
        
        // 本月
        $month = date('Y-m',time());
        // $month = input('month') ?? '';

        // 五个区
        $address = Db::name('address')->where('id','>',0)->order('weigh desc')->select()->toArray();
        //  七个产业
        $industry = Db::name('industry')->where('id','>',0)->order('weigh desc')->select()->toArray();

        foreach ($address as $k => $v) {
            foreach ($industry as $key => $value) {
                $count = $this->params_industry($v['id'],$value['id'],'','');
                $open = $this->params_industry($v['id'],$value['id'],'发明',3); //发明公开
                $auth = $this->params_industry($v['id'],$value['id'],'发明',1); //授权
                $news = $this->params_industry($v['id'],$value['id'],'实用新型',1);//新型
                $apper = $this->params_industry($v['id'],$value['id'],'外观设计',1);//外观设计
                $lose = $this->params_industry($v['id'],$value['id'],'',2); //失效
                $num = $auth + $news + $apper;
                $arr = [
                    'address_id'=>$v['id'],
                    'industry_id'=>$value['id'],
                    'date'=>$month,
                    'count'=>$count,
                    'open'=>$open,
                    'auth'=>$auth,
                    'news'=>$news,
                    'apper'=>$apper,
                    'lose'=>$lose,
                    'num'=>$num,
                    'createtime'=>date('Y-m-d H:i:s',time()),
                ];
                // var_dump($arr);exit;
                Db::name('data_industry')->insert($arr);
            }
        }
        return ajaxReturn('执行成功',1);
    }

    //=================================================================
    //企业的专利总数 直接跑
    public static function company_params($company)
    {
        $url = 'https://patent.shundeip.com/v1/search/expression';
        $data = [
            'exp'=>'申请人='.$company,
        ];
        $result = Http::post($url,$data);
        $result = json_decode($result,true);
        if (empty($result['data']) || empty($result['data']['page'])) {
            $total = 0;
        }
        $total = isset($result['data']['page']['total'])?$result['data']['page']['total']:0;
        return $total;
    }
    public function company_list()
    {
        // 本月
        $month = date('Y-m',time());
        // $month = input('month') ?? '';

        // 获取公司列表
        
        $company = Db::name('company')
                   ->field('id,company,address,industry')
                   ->where('id','>',0)
                   ->select()->toArray();

        foreach ($company as $k => $v) {
            // 根据名称来确定哪个区
            $address_id = 0;
            if (preg_match("/顺德/", $v['address'])) {
                $address_id = 1;
            }
            if (preg_match("/三水/", $v['address'])) {
                $address_id = 2;
            }
            if (preg_match("/高明/", $v['address'])) {
                $address_id = 3;
            }
            if (preg_match("/南海/", $v['address'])) {
                $address_id = 4;
            }
            if (preg_match("/禅城/", $v['address'])) {
                $address_id = 5;
            }
            $count = $this->company_params($v['company']);
            $arr = [
                'date'=>$month,
                'company'=>$v['company'],
                'address_id'=>$address_id,
                'industry'=>$v['industry'],
                'count'=>$count,
                'createtime'=>date('Y-m-d H:i:s',time()),
            ];
            // var_dump($arr);exit;
            Db::name('data_company')->insert($arr);
        }
        return ajaxReturn('执行成功',1);
    }


    // 直接获取某个公司的专利数据
    public static function companynews_params($company,$pdt,$lssc)
    {
        $url = 'https://patent.shundeip.com/v1/search/expression';
        // 判断是否有pdt和lssc
        $exp = '申请人='.$company;
        if (empty($pdt) && empty($lssc)) {
            $data = [
                'exp'=>$exp,
            ];
        }else if(empty($pdt)){
            $data = [
                'exp'=>$exp,
                'lssc'=>$lssc,
            ];
        }else if(empty($lssc)){
            $data = [
                'exp'=>$exp,
                'pdt'=>$pdt,
            ];
        }else{
            $data = [
                'exp'=>$exp,
                'pdt'=>$pdt,
                'lssc'=>$lssc,
            ];
        }
        $result = Http::post($url,$data);
        $result = json_decode($result,true);
        if (empty($result['data']) || empty($result['data']['page'])) {
            $total = 0;
        }
        $total = isset($result['data']['page']['total'])?$result['data']['page']['total']:0;
        return $total;
    }
    public function companynews_list()
    {
        $company = input('company') ?? '';

        $count = $this->companynews_params($company,'','');
        $open = $this->companynews_params($company,'发明',3); //发明公开
        $auth = $this->companynews_params($company,'发明',1); //授权
        $news = $this->companynews_params($company,'实用新型',1);//新型
        $apper = $this->companynews_params($company,'外观设计',1);//外观设计
        $lose = $this->companynews_params($company,'',2); //失效
        $industry = Db::name('data_company')->where('company',$company)->value('industry');
        $data = [
            'company'=>$company,
            'count'=>$count,
            'open'=>$open,
            'auth'=>$auth,
            'news'=>$news,
            'apper'=>$apper,
            'lose'=>$lose,
            'industry'=>$industry,
        ];
        $this->success('success',$data);
    }

    // 公司增量表
    // 本月减去上个月得到
    public function company_add_list()
    {
        // 本月
        $month = date('Y-m',time());
        $last_month = date('Y-m',strtotime("-1 month"));
        // $month = input('month') ?? '';
        // $last_month = input('last_month') ?? '';

        // 获取本月公司总量
        $company = Db::name('data_company')
                   ->where('date',$month)
                   ->select()->toArray();
        foreach ($company as $k => $v) {
            $list = Db::name('data_company')
                    ->where('date',$last_month)
                    ->where('company',$v['company'])
                    ->where('address_id',$v['address_id'])
                    ->find();
            $count = $list['count'];
            $arr = [
                'date'=>$month,
                'company'=>$v['company'],
                'address_id'=>$v['address_id'],
                'industry'=>$v['industry'],
                'count'=>$v['count'] - $count,
                'createtime'=>date('Y-m-d H:i:s',time()),
            ];
            Db::name('data_company_add')->insert($arr);
        }
        return ajaxReturn('执行成功',1);
    }

    //=================================================
    //顶尖团队专利情况数据
    public static function params_team($industry_id,$pdt,$lssc)
    {
        $url = 'https://patent.shundeip.com/v1/search/expression';
        // 拼接ipc
        $ipc = '';
        $no_ipc = '';
        $industry = Db::name('industry_ipc')
                    ->where('industry_id',$industry_id)
                    ->select()->toArray();
        foreach ($industry as $k => $v) {
            if ($v['is_remove']==0) {  //表示需要拼接的
                $temp = explode('、',$v['ipc']);
                foreach ($temp as $key => $value) {
                    $ipc .= 'IPC=\''.$value.'\' OR ';
                }
            }
            if ($v['is_remove']==1) {  //表示需要去除的
                $temp = explode('、',$v['ipc']);
                foreach ($temp as $key => $value) {
                    $no_ipc .= 'IPC=\''.$value.'\' OR ';
                }
            }
        }
        $ipc = substr_replace($ipc,'',-3);//去除最后的三个字符
        $no_ipc = substr_replace($no_ipc,'',-3);
        // var_dump($ipc);
        // var_dump($no_ipc);
        if (empty($no_ipc)) {
            $exp = '申请人地址=(\'大学\' OR \'学校\' OR \'学院\' OR \'研究院\') AND ('.$ipc.')';
        }else{
            $exp = '申请人地址=(\'大学\' OR \'学校\' OR \'学院\' OR \'研究院\') AND ('.$ipc.') NOT ('.$no_ipc.')';
        }
        // 判断是否有pdt和lssc
        if (empty($pdt) && empty($lssc)) {
            $data = [
                'exp'=>$exp,
            ];
        }else if(empty($pdt)){
            $data = [
                'exp'=>$exp,
                'lssc'=>$lssc,
            ];
        }else if(empty($lssc)){
            $data = [
                'exp'=>$exp,
                'pdt'=>$pdt,
            ];
        }else{
            $data = [
                'exp'=>$exp,
                'pdt'=>$pdt,
                'lssc'=>$lssc,
            ];
        }
        
        // var_dump($data);exit;
        $result = Http::post($url,$data);
        $result = json_decode($result,true);
        if (empty($result['data']) || empty($result['data']['page'])) {
            $total = 0;
        }

        $total = isset($result['data']['page']['total'])?$result['data']['page']['total']:0;
        // var_dump($total);exit;
        return $total;
    }

    public function team_list()
    {
        
        // 本月
        $month = date('Y-m',time());
        // $month = input('month') ?? '';

        
        //  七个产业
        $industry = Db::name('industry')->where('id','>',0)->order('weigh desc')->select()->toArray();

        
        foreach ($industry as $k => $v) {
            $count = $this->params_team($v['id'],'','');
            $open = $this->params_team($v['id'],'发明',3); //发明公开
            $auth = $this->params_team($v['id'],'发明',1); //授权
            $news = $this->params_team($v['id'],'实用新型',1);//新型
            $apper = $this->params_team($v['id'],'外观设计',1);//外观设计
            $lose = $this->params_team($v['id'],'',2); //失效
            $num = $auth + $news + $apper;
            $arr = [
                'industry_id'=>$v['id'],
                'date'=>$month,
                'count'=>$count,
                'open'=>$open,
                'auth'=>$auth,
                'news'=>$news,
                'apper'=>$apper,
                'lose'=>$lose,
                'num'=>$num,
                'createtime'=>date('Y-m-d H:i:s',time()),
            ];
            // var_dump($arr);exit;
            Db::name('data_team')->insert($arr);
        }
        
        return ajaxReturn('执行成功',1);
    }


    //====================================================================
    //=================================================
    // 顶尖团队的第二种算法
    // 需要统计出团队的数量和排名
    // 第一步：先跑出这个月的专利存下来
    // 第二步：通过aspo进行拆分团队，进而统计团队数量
    // 第三步：对团队数量进行排名等等
    public static function params_team_two($industry_id,$pdt,$lssc,$page,$start,$end)
    {
        $url = 'https://patent.shundeip.com/v1/search/expression';
        // 拼接ipc
        $ipc = '';
        $no_ipc = '';
        $industry = Db::name('industry_ipc')
                    ->where('industry_id',$industry_id)
                    ->select()->toArray();
        foreach ($industry as $k => $v) {
            if ($v['is_remove']==0) {  //表示需要拼接的
                $temp = explode('、',$v['ipc']);
                foreach ($temp as $key => $value) {
                    $ipc .= 'IPC=\''.$value.'\' OR ';
                }
            }
            if ($v['is_remove']==1) {  //表示需要去除的
                $temp = explode('、',$v['ipc']);
                foreach ($temp as $key => $value) {
                    $no_ipc .= 'IPC=\''.$value.'\' OR ';
                }
            }
        }
        $ipc = substr_replace($ipc,'',-3);//去除最后的三个字符
        $no_ipc = substr_replace($no_ipc,'',-3);
        // var_dump($ipc);
        // var_dump($no_ipc);
        // 申请人地址=广东 AND (申请人地址='大学' OR '学校' OR '学院' OR '研究院') AND (法律公告日>20230101)
        if (empty($no_ipc)) {
            $exp = '申请人地址=广东 AND (申请人=\'大学\' OR \'学校\' OR \'学院\' OR \'研究院\') AND (法律公告日>'.$start.') AND (法律公告日<'.$end.') AND ('.$ipc.')';
            // $exp = '申请人地址=(\'大学\' OR \'学校\' OR \'学院\' OR \'研究院\') AND ('.$ipc.')';
        }else{
            $exp = '申请人地址=广东 AND (申请人=\'大学\' OR \'学校\' OR \'学院\' OR \'研究院\') AND (法律公告日>'.$start.') AND (法律公告日<'.$end.') AND ('.$ipc.') NOT ('.$no_ipc.')';
            // $exp = '申请人地址=(\'大学\' OR \'学校\' OR \'学院\' OR \'研究院\') AND ('.$ipc.') NOT ('.$no_ipc.')';
        }
        // 判断是否有pdt和lssc
        if (empty($pdt) && empty($lssc)) {
            $data = [
                'exp'=>$exp,
                'page'=>$page,
            ];
        }else if(empty($pdt)){
            $data = [
                'exp'=>$exp,
                'lssc'=>$lssc,
            ];
        }else if(empty($lssc)){
            $data = [
                'exp'=>$exp,
                'pdt'=>$pdt,
            ];
        }else{
            $data = [
                'exp'=>$exp,
                'pdt'=>$pdt,
                'lssc'=>$lssc,
            ];
        }
        
        // var_dump($data);exit;
        $result = Http::post($url,$data);
        $result = json_decode($result,true);
        if (empty($result['data']) || empty($result['data']['page'])) {
            $total = 0;
            $list = [];
        }

        $total = isset($result['data']['page']['total'])?$result['data']['page']['total']:0;
        $list = isset($result['data']['patent_list'])?$result['data']['patent_list']:[];
        // var_dump($result);exit;
        $end = [
            'total'=>$total,
            'list'=>$list,
        ];
        return $end;
    }

    public function team_list_two()
    {
        
        // 本月
        $month = date('Y-m',time());
        // $month = input('month') ?? '';
        // 获取法律公告日
        $end = date('Ym01',time());
        $start = date('Ym01',strtotime('-1 year'));

        
        //  七个产业
        //  跑数据需要断点的地方 注意产业id
        $industry = Db::name('industry')->where('id','=',8)->order('weigh desc')->select()->toArray();

        $arr = [];
        foreach ($industry as $k => $v) {
            // 请求第一次确定有多少页数
            $list = $this->params_team_two($v['id'],'','',1,$start,$end);
            // $arr = $list['list'];
            // foreach($arr as &$temp){
            //     $temp['inc_one'] = explode(';',$temp['inc'])[0];
            //     $temp['industry_id'] = $v['id'];
            //     $temp['page'] = 1;
            //     $temp['date'] = $month;
            //     $temp['create_time'] = date('Y-m-d H:i:s',time());
            // }
            // var_dump($list);
            // exit;
            // Db::name('data_base')->insertAll($arr);
            $page = ceil($list['total']/10);

            // 如果只有一页就不用继续了 超过1页循环入库
            if ($page>=1) {
                for ($i=1; $i <= $page; $i++) { 
                    $list_two = $this->params_team_two($v['id'],'','',$i,$start,$end);
                    $arr = $list_two['list'];
                    foreach($arr as &$temp){
                        $temp['inc_one'] = isset(explode(';',$temp['inc'])[0]) ? explode(';',$temp['inc'])[0] : '';
                        $temp['industry_id'] = $v['id'];
                        $temp['page'] = $i;
                        $temp['date'] = $month;
                        $temp['create_time'] = date('Y-m-d H:i:s',time());
                    }
                    // 全部入库
                    Db::name('data_base')->insertAll($arr);
                }
            }
            
        }
        
        return ajaxReturn('执行成功',1);
    }

    // 统计团队的数量
    // 在 data_base 表里拆出团队和专利数量
    // 第一步：将所有团队整理出来
    // 第二步：计算每个团队的专利数量
    public function rank_team()
    {
        // 本月
        $month = date('Y-m',time());
        $result = [];
        Db::startTrans();
        try {

            // 删除重复
            // $a = array_column(Db::name('data_base_team')
            // ->field('id')
            // ->where('date','=',$month)
            // ->group('team_name,aspo,industry_id')
            // ->having('count(*)>1')
            // ->select()->toArray(),'id');
            // var_dump($a);exit;
            // Db::name('data_base_team')->delete($a);

            // 删除无效的专利
            // $id_arr = array_column(Db::name('data_base')
            //             ->field('id')
            //             ->where('date',$month)
            //             ->where('status','无效')
            //             ->select()->toArray(),'id');
            // Db::name('data_base')->delete($id_arr);
            // 第一步：创建团队
            // 需要配合inc_one 和 aspo 确定一条记录
            // Db::name('data_base')
            //     ->where('date','=',$month)
            //     ->chunk(1000,function($user) use($month) {
            //     $result = [];
            //     foreach ($user as $k => $v) {
            //         // 获取全部团队
            //         $temp = explode(';',$v['aspo']);
            //         if (!empty($temp)) {
            //             foreach ($temp as $k1 => $v1) {
            //                 $temp[$k1] = $v1.'-'.$v['industry_id'].'-'.$v['inc_one'];
            //             }
            //         }
            //         $result = array_merge($result,$temp);
            //     }
                
            //     // 去重
            //     $result = array_unique($result);
            //     $arr = Db::name('data_base_team')
            //             ->where('date',$month)
            //             ->select()->toArray();
            //     // 需要将数组转化成已有格式   团队领头人-所属高校
            //     $arr_result = [];
            //     foreach ($arr as $k => $v) {
            //         $arr_result[$k] = $v['aspo'].'-'.$v['industry_id'].'-'.$v['team_name'];
            //     }
            //     // 获取result和arr_result中不同的团队进行入库
            //     $diff = array_diff($result, $arr_result);
            //     $end_arr = [];
            //     foreach ($diff as $k => $v) {
            //         // $end_arr[$k]['date'] = $month;
            //         // 需要拆开 分成团队领头人 产业 所属高校
            //         $ttt = explode('-',$v);

            //         // 拆分开了之后没有 大学' OR '学校' OR '学院' OR '研究院 去除
            //         if(preg_match("/大学/", $ttt['0']) || preg_match("/学校/", $ttt['0']) || preg_match("/学院/", $ttt['0']) || preg_match("/研究院/", $ttt['0']) ) {
            //             // 没有条件的去除
            //             $end_arr[$k]['date'] = $month;
            //             $end_arr[$k]['team_name'] = isset($ttt[0]) ? $ttt[0] : '';
            //             $end_arr[$k]['aspo'] = isset($ttt[2]) ? $ttt[2] : '';
            //             $end_arr[$k]['industry_id'] = isset($ttt[1]) ? $ttt[1] : '';
            //             $end_arr[$k]['create_time'] = date('Y-m-d H:i:s',time());
            //         }else{
            //             // $end_arr[$k]['team_name'] = isset($ttt[0]) ? $ttt[0] : '';
            //             // $end_arr[$k]['aspo'] = isset($ttt[2]) ? $ttt[2] : '';
            //             // $end_arr[$k]['industry_id'] = isset($ttt[1]) ? $ttt[1] : '';
            //             // $end_arr[$k]['create_time'] = date('Y-m-d H:i:s',time());
            //         }
            //         // var_dump($end_arr);exit;
            //     }
            //     // var_dump($result);
            //     // var_dump($diff);
            //     // var_dump($end_arr);exit;
            //     Db::name('data_base_team')->insertAll($end_arr);
            // });
            // var_dump(111);exit;
            // 第二步：加专利数据
            // Db::name('data_base_team')
            //     ->where('date','=',$month)
            //     ->where('id','>',25000)
            //     ->where('id','<=',26000)
            //     ->chunk(100,function($user) use($month) {
            //     $result = [];
            //     foreach ($user as $k => $v) {
            //         // 获取所属高校 团队领头人 直接计算总数
            //         $num = Db::name('data_base')
            //         ->where('date',$month)
            //         ->where('inc_one',$v['aspo'])
            //         ->where('aspo','like','%'.$v['team_name'].'%')
            //         ->where('industry_id',$v['industry_id'])
            //         ->count();
            //         $auth = Db::name('data_base')
            //         ->where('date',$month)
            //         ->where('inc_one',$v['aspo'])
            //         ->where('aspo','like','%'.$v['team_name'].'%')
            //         ->where('industry_id',$v['industry_id'])
            //         ->where('pdt','发明')
            //         ->count();
            //         $news = Db::name('data_base')
            //         ->where('date',$month)
            //         ->where('inc_one',$v['aspo'])
            //         ->where('aspo','like','%'.$v['team_name'].'%')
            //         ->where('industry_id',$v['industry_id'])
            //         ->where('pdt','实用新型')
            //         ->count();
            //         $apper = Db::name('data_base')
            //         ->where('date',$month)
            //         ->where('inc_one',$v['aspo'])
            //         ->where('aspo','like','%'.$v['team_name'].'%')
            //         ->where('industry_id',$v['industry_id'])
            //         ->where('pdt','外观设计')
            //         ->count();
            //         // var_dump($num);
            //         // var_dump($auth);
            //         // var_dump($open);
            //         // var_dump($apper);exit;
            //         Db::name('data_base_team')
            //         ->where('id',$v['id'])
            //         ->update([
            //             'num'=>$num,
            //             'auth'=>$auth,
            //             'news'=>$news,
            //             'apper'=>$apper,
            //         ]);
            //     }
            // });
            

            // 第三步：将数据整理成原先展示表格信息
            //  七个产业
            // $industry = Db::name('industry')->where('id','>',0)->order('weigh desc')->select()->toArray();
            // $arr_three = [];
            // foreach ($industry as $k => $v) {
            //     $arr_three = Db::name('data_base_team')->field('sum(num) as num,sum(auth) as auth,sum(news) as news,sum(apper) as apper')->where('date',$month)->where('industry_id',$v['id'])->find();

            //     $arr_three_temp = [
            //         'industry_id'=>$v['id'],
            //         'date'=>$month,
            //         'count'=>$arr_three['num'],
            //         'open'=>0,
            //         'auth'=>$arr_three['auth'],
            //         'news'=>$arr_three['news'],
            //         'apper'=>$arr_three['apper'],
            //         'lose'=>0,
            //         'num'=>$arr_three['num'],
            //         'createtime'=>date('Y-m-d H:i:s',time()),
            //     ];
            //     // var_dump($arr_three_temp);exit;
            //     Db::name('data_base_team_add')->insert($arr_three_temp);
            // }
                
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            return $e->getMessage();
        }
        return ajaxReturn('执行成功',1);
    }
    

    // 用于测试定时任务
    // 给test表添加数据 一分钟添加一条
    public function test()
    {
        $month = date('Y-m-d H:i:s',time());
        $list = Db::name('test')->where('id',1)->update(['title'=>$month]);
        return ajaxReturn('执行成功',1);
    }



}